import Swiper from 'swiper'

let SwiperInfo = null;

const SwiperFn = (delay) => {
    let swiperInfo2 = new Swiper('.swiper-container', {
        direction: 'horizontal',
        loop: false,
        slidesPerView: "auto",
        centeredSlides: true,
        spaceBetween: 1,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            type: 'fraction'
        },
        on: {
            slideChange: () => {
                console.log('滑动结束')
                swiperInfo2.update();
            },
        },
    });
    return SwiperInfo = swiperInfo2;
};

export default {

    binding(el, binding, vnode) {
        // console.log('binding');
    },

    inserted(el, binding, vnode) {
        let {delay,index,length} = binding.value;
        if (!SwiperInfo) {
            SwiperInfo = SwiperFn(delay || 5000);
        } else {
            SwiperInfo.update();
        }
    },

    updated(el, binding, vnode, oldVnode) {
        let {delay,index,length} = binding.value;
        if (!SwiperInfo) {
            SwiperInfo = SwiperFn(delay || 5000);
        } else {
            SwiperInfo.update();
        }
    },

    componentUpdated(el, binding, vnode, oldVnode) {
        let {delay,index,length} = binding.value;
        if (!SwiperInfo) {
            SwiperInfo = SwiperFn(delay || 5000);
        } else {
            SwiperInfo.update();
        }
    },

    unbind(el, binding, vnode) {
        SwiperInfo = null
    }
}